package com.hcbd.tdwt.ui.xingyu.fragment;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.hcbd.tdwt.R;
import com.hcbd.tdwt.base.BaseFragment;
import com.hcbd.tdwt.base.BaseSpinner;
import com.hcbd.tdwt.base.BeidouBoxParams;

import com.hcbd.tdwt.base.hcbd.CCPRSHcbdInfo;
import com.hcbd.tdwt.base.hcbd.HcbdProtocol;
import com.pangu.bdsdk2021.bluetooth.BluetoothManage;
import com.pangu.bdsdk2021.terminal.TerminalSendManager;


import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.ref.WeakReference;
import java.text.MessageFormat;

@SuppressLint("NonConstantResourceId")
@EFragment(R.layout.fragment_bd_location_upload)
public class BdLocationUpFragment  extends BaseFragment {
    @ViewById
    TextView frequencyTipsTV;
    @ViewById
    EditText frequencyET,centerNumber;
    @ViewById
    Spinner samplingNumberSp,protocolTypeSp,posModeSp;

    @ViewById
    TextView startBtn;
    BaseSpinner baseSpinner;
    Drawable ovaBlueDrawable;
    Drawable ovaGrayDrawable;
    Drawable ovaRedDrawable;
    private boolean isOpen=false;
    private HcbdProtocol hcbdProtocol=new HcbdProtocol();
    @AfterViews
    protected void init() {
        initMsgRegister();
        myHandler=new MyHandler(this);
        baseSpinner = new BaseSpinner(this.getActivity());
        baseSpinner.initSprinnerByKey(samplingNumberSp, R.array.sampling_number, R.array.sampling_number, "0");

        baseSpinner.initSprinnerByKey(protocolTypeSp, R.array.protocol_type_value, R.array.protocol_type_key, "0");

        baseSpinner.initSprinnerByKey(posModeSp,R.array.pos_type_value , R.array.pos_type_key, "0");

        ovaBlueDrawable=getResources().getDrawable(R.drawable.ova_light_blue_bg);
        ovaRedDrawable=getResources().getDrawable(R.drawable.ova_red_bg2);
        ovaGrayDrawable=getResources().getDrawable(R.drawable.ova_gray_bg);
        String text = "当前北斗卡服务频度为";
        frequencyTipsTV.setText(MessageFormat.format("{0}{1}秒", text, BeidouBoxParams.sendFreq));
        initData();
    }
    public void initData(){
        if(BeidouBoxParams.isBoxConnectNormal){
            startBtn.setClickable(true);
            startBtn.setText("启动");
            startBtn.setBackground(ovaBlueDrawable);
            myHandler.sendEmptyMessage(1);

        }else{
            startBtn.setClickable(false);
            startBtn.setText("未连接");
            startBtn.setBackground(ovaGrayDrawable);
        }
    }
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true, priority = 1)
    public void eventBus(CCPRSHcbdInfo ccprsInfo){
        frequencyET.setText(ccprsInfo.acquisition_frequency);
        centerNumber.setText(ccprsInfo.receiving_address);
        baseSpinner.initSprinnerByKey(samplingNumberSp,R.array.sampling_number, R.array.sampling_number,ccprsInfo.messages_number);
        baseSpinner.initSprinnerByKey(protocolTypeSp,R.array.protocol_type_value, R.array.protocol_type_key,ccprsInfo.protocol_type);
        if("0".equals(ccprsInfo.reserved_domain)||"0".equals(ccprsInfo.messages_number)){
            isOpen=false;
            startBtn.setClickable(true);
            startBtn.setText("启动");
            startBtn.setBackground(ovaBlueDrawable);

        }else{
            isOpen=true;
            startBtn.setClickable(true);
            startBtn.setText("关闭");
            startBtn.setBackground(ovaRedDrawable);
        }
    }

    @Click
    void startBtn(){
        String number=getTextStr(centerNumber);
        if("".equals(number)){
            toast("请输入中心号码!");
            return;
        }
        String frequency=getTextStr(frequencyET);
        if("".equals(frequency)){
            toast("请输入采样频度!");
            return;
        }
        String samplingNum=baseSpinner.getSpinnerValue(samplingNumberSp);
        if("0".equals(samplingNum)){
            toast("采样个数为0则关闭上报功能!");
            return;
        }
        String proType=baseSpinner.getSpinnerKey(protocolTypeSp);
        CCPRSHcbdInfo info=new CCPRSHcbdInfo();
        info.receiving_address=number;
        info.acquisition_frequency=frequency;
        info.protocol_type=proType;
        if(isOpen){
            info.reserved_domain="0";
            info.messages_number="0";
        }else{
            info.reserved_domain="3";
            info.messages_number=samplingNum;
        }

        BluetoothManage.getInstance().write(hcbdProtocol.setCCPRSInfo(info));
    }
    private MyHandler myHandler;
    private static class MyHandler extends Handler {
        //弱引用，在垃圾回收时，被回收
        WeakReference<BdLocationUpFragment> fragmentWeakReference;
        MyHandler(BdLocationUpFragment fragment){
            this.fragmentWeakReference=new WeakReference<>(fragment);
        }
        public void handleMessage(Message msg){
            TerminalSendManager.getInstance().level_two_getCCPRQInfo();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        myHandler.removeCallbacksAndMessages(null);
    }
}
